package com.example.italkapp2.model;

import android.content.Context;

import com.example.italkapp2.model.bean.UserInfo;
import com.example.italkapp2.model.dao.UserAccountDao;
import com.example.italkapp2.model.db.DBManager;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

//数据模型层全局类
public class Model {

    private Context mContext;
    private UserAccountDao userAccountDao;
    private DBManager dbManager;

    //全局线程池
    private ExecutorService executors= Executors.newCachedThreadPool();

    //创建对象
    private static Model model=new Model();

    //私有化构造
    private Model() {
    }

    //获取单例对象
    public static Model getInstance(){
        return model;
    }

    //初始化方法，获取上下文
    public void init(Context context){
        mContext=context;

        //创建用户账号数据库的操作类对象
        userAccountDao = new UserAccountDao(mContext);

        //开启全局监听
        EventListener eventListener = new EventListener(mContext);
    }

    //获取全局线程池
    public ExecutorService getGlobalThreadPool(){
        return executors;
    }


    //登陆成功后的处理
    public void loginSuccess(UserInfo account){

        //校验
        if(account==null){
            return;
        }

        if(dbManager!=null){
            dbManager.close();
        }
        dbManager = new DBManager(mContext,account.getName());
    }

    //数据要经过模型层,获取用户账号数据库的操作类对象
    public UserAccountDao getUserAccountDao(){
        return userAccountDao;
    }

    public DBManager getDbManager() {
        return dbManager;
    }

    public void setDbManager(DBManager dbManager) {
        this.dbManager = dbManager;
    }
}
